package client.comm.baoding.adapter

import android.content.Context
import android.graphics.Color
import android.text.Layout
import android.util.TypedValue
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import client.comm.commlib.base.*
import client.comm.baoding.R
import client.comm.baoding.api.bean.BuyCartRet
import client.comm.baoding.databinding.LayoutNewbuycartitemBinding
import client.comm.baoding.utils.TextTool
import com.bumptech.glide.RequestManager

class NewBuyCartAdapter(val context: Context, val manager: RequestManager) :
    BasePagingAdapter<BuyCartRet.Shopping>(object : DiffUtil.ItemCallback<BuyCartRet.Shopping>() {
        override fun areItemsTheSame(
            oldItem: BuyCartRet.Shopping,
            newItem: BuyCartRet.Shopping
        ): Boolean {
            return oldItem.goods_id == newItem.goods_id
        }

        override fun areContentsTheSame(
            oldItem: BuyCartRet.Shopping,
            newItem: BuyCartRet.Shopping
        ): Boolean {
            return oldItem == newItem
        }
    }) {

    var changeCheckItem: ((bean: BuyCartRet.Shopping) -> Unit)? = null
    var detailItem: ((bean: BuyCartRet.Shopping) -> Unit)? = null
    var changeNumItem: ((bean: BuyCartRet.Shopping, num: Int) -> Unit)? = null
    var textWatcher: SimpleTextWatcher? = null

    fun changeCheckClick(bean: BuyCartRet.Shopping) {
        changeCheckItem?.invoke(bean)
    }
    fun changeNum(bean: BuyCartRet.Shopping, num: Int) {
        changeNumItem?.invoke(bean, num)
    }

    fun onGoDetail(bean: BuyCartRet.Shopping) { //跳详情
        detailItem?.invoke(bean)
    }

    fun getItemObj(pos: Int): BuyCartRet.Shopping? {
        return getItem(pos)
    }

    override fun bindItem(holder: PagingViewHoder, t: Any) {
        val binding = holder.binding as LayoutNewbuycartitemBinding
        val bean = t as BuyCartRet.Shopping

        binding.bean = bean
        manager.load(bean.goods_img).into(binding.img)

        binding.commPriceIntegral.apply {
            comm_price_integral(tvPriceUnit,tvPrice,bean.price,bean.lpq_price,tvIntegral,bean.integral,tvIntegralDesc)
        }

        binding.event = this
    }

    override fun getItemLayout(): Int {
        return R.layout.layout_newbuycartitem
    }

    /**
     * 通用商品价格和积分格式化
     */
    fun comm_price_integral(
        tvPriceUnit: TextView,
        tvPrice: TextView,
        price: Double,
        lpq_price: Double,
        tvIntegral: TextView,
        integral: Double,
        tvIntegralDesc: TextView
    ) {
        var is_price_show: Boolean = false
        var is_integral_unit_show: Boolean = false

        if (price > 0) {
            tvPrice.text = "${price}"
            is_price_show = true
            if (integral > 0) {
                tvIntegral.text = "+${integral}" //+ "积分"
                is_integral_unit_show = true
            } else {
                tvIntegral.text = ""
                is_integral_unit_show = false
            }

        } /*else if (lpq_price > 0) {
            TextTool.getBuilder("${lpq_price}")
                .setAlign(Layout.Alignment.ALIGN_CENTER).setTextSize(18)
                .append("体验券").setForegroundColor(Color.parseColor("#E81837"))
                .setTextSize(12)
                .into(tvPrice)
            is_price_show = true
            if (integral > 0) {
                tvIntegral.text = "+" + integral //+ "积分"
                is_integral_unit_show = true
            } else {
                tvIntegral.text = ""
                is_integral_unit_show = false
            }

        }*/ else {
            tvPrice.text = ""
            is_price_show = false
            if (integral > 0) {
                tvIntegral.text = "" + integral //+ "积分"
                is_integral_unit_show = true
            } else {
                tvIntegral.text = ""
                is_integral_unit_show = false
            }
        }

        if (is_price_show) {
            tvPriceUnit.visibility = View.VISIBLE
        } else {
            tvPriceUnit.visibility = View.GONE
        }

        if (is_integral_unit_show) {
            tvIntegralDesc.visibility = View.VISIBLE
        } else {
            tvIntegralDesc.visibility = View.GONE
        }
        tvPrice.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16F)
        tvIntegral.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16F)
    }

}